#include "hm_tab.h"
#include <EIKENV.H>
/*
 * 用来控制tab的类
 * 
 */
ChmTab::ChmTab()
{
	iTabShow = 0;
	iTabCur = 0;
	iTabNum = 0;
	iOffset = 0;
	iControl = NULL;
}

ChmTab::~ChmTab()
{
	ClearTabData();
}

void ChmTab::Init(CHtmlControl* aControl, int aTabShownum, int aSelTab)
{
	iTabCur = aSelTab;
	iOffset = 0;
	iTabShow = aTabShownum;
	iTabNum = iAppTabData.Count();
	
	if(iTabNum == 0)return;
	iControl = aControl;		
	
	CHtmlElement* itemcontainer = iControl->Element(_L("iTopTab"));
	itemcontainer->SetStyle(KHStrDisplay, KHStrVisible);			
	if(itemcontainer == NULL)
		return;
	else
		itemcontainer->SetProperty(KHStrInnerHtml, KNullDesC);
	
	_LIT(KCatHtmlLeft, "<div id='apptab_left' class='cat'><img src='mem.tableft.gif'></div>");	
	_LIT(KCatHtmlRight, "<div id='apptab_right' class='cat'><img src='mem.tabright.gif'></div>");	
	
//	_LIT(KCatHtml, "<div id='apptab' focusing='true' class='cat' focus-class='focus'></div>");
	_LIT(KCatHtml, "<div id='apptab' focusing='false' class='tabitem' focus-class='tabfocus'></div>");
		
	//load all tabs including start and end indicator
	CHtmlElement* e1 = iControl->InsertContentL(iControl->Element(_L("iTopTab")), EBeforeEnd, KCatHtmlLeft);	
	e1->SetStyle(KHStrDisplay, KHStrNone);	

	for(TInt i=0;i<iTabNum;i++)
	{
		CHtmlElement* cat = iControl->InsertContentL(iControl->Element(_L("iTopTab")), EBeforeEnd, KCatHtml);
		MY_TAB_DATA* data = iAppTabData[i];
		cat->SetProperty(KHStrInnerHtml, data->tabtitle);
	}
	
	for(int i = iTabShow; i < iTabNum; i++)
	{
		CHtmlElement* itemcontainer = iControl->Element(_L("iTopTab"));
		CHtmlElement* item = itemcontainer->Element(_L("apptab"),i);
		item->SetStyle(KHStrDisplay, KHStrNone);
	}
	
	CHtmlElement* e2 = iControl->InsertContentL(iControl->Element(_L("iTopTab")), EBeforeEnd, KCatHtmlRight);	

	if(iTabNum <= iTabShow)
		e2->SetStyle(KHStrDisplay, KHStrNone);	
		
	CHtmlElement* e = itemcontainer->Element(_L("apptab"),aSelTab);
	
	e->SetProperty(KHStrClass, _L("tabfocus"));				
////	iControl->SetFocusTo(e);		
}

void ChmTab::ShowTabsChange()
{
	CHtmlElement* itemcontainer = iControl->Element(_L("iTopTab"));
	for(int i = 0; i < iTabNum; i++)
	{
		CHtmlElement* item = itemcontainer->Element(_L("apptab"),i);
		item->SetStyle(KHStrDisplay, KHStrNone);//KHStrVisible);
	}

	for(int i = iOffset; i < iOffset + iTabShow; i++)
	{
		CHtmlElement* item = itemcontainer->Element(_L("apptab"),i);
		item->SetStyle(KHStrDisplay, KHStrVisible);
	}

	//处理左右指示符
	CHtmlElement* item1 = itemcontainer->Element(_L("apptab_left"));
	if(iTabCur == 0)
		item1->SetStyle(KHStrDisplay, KHStrNone);
	else
		item1->SetStyle(KHStrDisplay, KHStrVisible);
	
	CHtmlElement* item2 = itemcontainer->Element(_L("apptab_right"));
	if(iTabCur == iTabShow)
		item2->SetStyle(KHStrDisplay, KHStrNone);
	else
		item2->SetStyle(KHStrDisplay, KHStrVisible);
}

bool ChmTab::ProcessTabChange(bool bNextPrev)
{
	if(iTabNum == 0)
	return false;
	
	CHtmlElement* itemcontainer = iControl->Element(_L("iTopTab"));
	if(itemcontainer == NULL) return false;
	TBuf<20> displayed;
	itemcontainer->GetStyle(KHStrDisplay, displayed);
	if(displayed == _L("none")) return false; //not displayed.
	
	int old = iTabCur;
	if(bNextPrev)
	{
		if(iTabCur == iTabNum-1)
			return false;
		else
			iTabCur ++;
		if((iTabCur - iOffset) >= iTabShow){
			iOffset ++;
			ShowTabsChange();
		}
	}
	else
	{
		if(iTabCur == 0)
			return false;
		else
			iTabCur -- ;
		if(iTabCur < iOffset){
			iOffset --;
			ShowTabsChange();
		}
	}

	CHtmlElement* olditem = itemcontainer->Element(_L("apptab"),old);
	olditem->SetProperty(KHStrClass, _L("tabitem"));
	
	CHtmlElement* item1 = itemcontainer->Element(_L("apptab"),iTabCur);
	item1->SetProperty(KHStrClass, _L("tabfocus"));
	
	iControl->RefreshAndDraw();	
	
	return true;
}

void ChmTab::ShowTab()
{
	if(iControl == NULL) return;

	CHtmlElement* itemcontainer = iControl->Element(_L("iTopTab"));
	if(itemcontainer == NULL) return;

	itemcontainer->SetStyle(KHStrDisplay, KHStrVisible);	
}

void ChmTab::HideTab()
{
	if(iControl == NULL) return;
	CHtmlElement* itemcontainer = iControl->Element(_L("iTopTab"));
	if(itemcontainer == NULL) return;
	itemcontainer->SetStyle(KHStrDisplay, KHStrNone);		
}

void ChmTab::ClearTabData()
{
	if(iControl == NULL)
	return;
	for(int i = 0; i < iAppTabData.Count(); i++)
	{
		MY_TAB_DATA* data = (MY_TAB_DATA*)iAppTabData[i];
		delete data;
	}

	iAppTabData.Reset();
	
	//div
	CHtmlElement* itemcontainer = iControl->Element(_L("iTopTab"));
	if(itemcontainer)
		itemcontainer->SetProperty(KHStrInnerHtml, KNullDesC);
}
